//
//  LanguageSelectViewController.m
//  Lightthings
//
//  Created by 王岩 on 2024/12/5.
//

#import "LanguageSelectViewController.h"

@interface LanguageSelectViewController ()
/* <#note#> */
@property (nonatomic, assign) int index;
@end

@implementation LanguageSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"语言设置");
    
    self.collectionInsets =  kEdge(self.jy_bar_h, 0, klSafeAreaEdgeInsets.bottom + 70, 0);
    
    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    
    [SWInitializer jy_initRedButtonWithTarget:self action:@selector(clickSureBtnAction) frame:CGRectMake(0, 0, kScreenWidth - 100, 50) title:SWString(@"Confirm") on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(50);
        kMakeBV(-klSafeAreaEdgeInsets.bottom - 10);
        kMakeWHV(kScreenWidth-100, 50);
    }];
    
    
    MainSectionModel * s = [[MainSectionModel alloc] init];
    NSArray * arr = @[@"简体中文", @"繁体中文", @"英语", @"西班牙语", @"俄语", @"日语", @"韩语", @"法语", @"葡萄牙语"];
    for (int i = 0; i < arr.count; i ++) {
        TitleModel * m = [TitleModel cModelT:arr[i] s:CGSizeMake(kScreenWidth, 50)];
        m.idfBlock = ^NSString * _Nonnull{
            return @"LanguageSelectCell";
        };
        m.type = i;
        m.value = @(NO);
        m.action = kMsgPackForward(@"clickItem:", WKProxy(self), @(i));
        [s.items addObject:m];
    }
    
    self.dataSources = @[s];
    [self readData];
    // Do any additional setup after loading the view.
}

- (void)readData {
    [WatchCommandTool.sharedInstance writeStormCommand:Command_LanguageSet ToPeripheral:self.peripheral Value:nil reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
            unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
            NSLog(@"语言数值：%lu",value);
            self.index = value;
            [self updateUI:value];
        }
    }];
}

- (void)clickItem:(NSNumber *)i {
    [self updateUI:i.intValue];
}

- (void)updateUI:(int)i {
    for (TitleModel * m in self.dataSources.firstObject.items) {
        if (m.type == i) {
            m.value =@(YES);
            self.index = i;
        }else {
            m.value = @(NO);
        }
    }
    [self reloadIfNeeded];
}

- (void)clickSureBtnAction {
    unsigned char byte1 = (self.index & 0xff);

    [[WatchCommandTool sharedInstance] writeStormCommand:Command_LanguageSet ToPeripheral:self.peripheral Value:@[@(byte1)] reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            if ([backData hasSuffix:@"00"]) {
                toast(SWString(@"Success"));
            }else {
                toast(SWString(@""));
            }
//            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
